﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.Equipment.Consum
{
    public partial class consumrecipients : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            tbRecipientsDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
            strPagePrivilege.Add("HCLY");//必须写在ispostback外
            strPageUserType.Add("mana");
            if (!Page.IsPostBack)
            {
                bind();
                tbCNub.Attributes.Add("readOnly", "-1");
                tbRPersion.Attributes.Add("readOnly", "-1");
                tbLabName.Attributes.Add("readOnly", "-1");
                tbRecipientsDate.Attributes.Add("readOnly", "-1");
                lbCName.Attributes.Add("readOnly", "-1");
                lbTypeNub.Attributes.Add("readOnly", "-1");
                lbAllNub.Attributes.Add("readOnly", "-1");
            }
        }
        public void bind()
        {
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();

            listCClass = eDictionary.GetItemListByDClass("shiyongfangxiang");
            ddlArrow.DataSource = listCClass;
            ddlArrow.DataTextField = "Name";
            ddlArrow.DataValueField = "Code";
            ddlArrow.DataBind();

            tbRecipientsDate.Text = DateTime.Now.Date.ToShortDateString();
        }

        protected void Submit_Click(object sender, EventArgs e)
        {

            #region 验证
            if (string.IsNullOrEmpty(tbCNub.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "耗材编号不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(tbUserNub.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用数量不能为空!");
                return;
            }
            string strTotalNub = lbAllNub.Text.Trim();
            decimal iTotalNub = 0;
            decimal.TryParse(strTotalNub, out iTotalNub);

            string strUseNub = tbUserNub.Text.Trim();
            decimal iUserNub = 0;
            decimal.TryParse(strUseNub, out iUserNub);

            if (iUserNub > iTotalNub)
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "库存不足!");
                return;
            }

            if (string.IsNullOrEmpty(tbRPersion.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用人不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(tbLabName.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用实验室不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(tbRecipientsDate.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用时间不能为空!");
                return;
            }

            #endregion
            ExtendBLL.SuppliesRecipients EbSRecipient = new ExtendBLL.SuppliesRecipients();
            LabMS.Model.SuppliesRecipients mSRecipient = new LabMS.Model.SuppliesRecipients();
            mSRecipient.Comment = tbRemarks.Text.Trim();
            mSRecipient.Arrow = ddlArrow.SelectedValue;

            mSRecipient.UnitPrice = "0";
            if (tbUintPrice.Value.Trim().Equals(""))
            {
                mSRecipient.UnitPrice = "0";
            }
            else
            {
                mSRecipient.UnitPrice = tbUintPrice.Value.Trim();
            }
            DateTime tempTime = new DateTime();
            if (DateTime.TryParse(tbRecipientsDate.Text.Trim(), out tempTime))
            {
                mSRecipient.EncodedTime = tempTime;
            }
            mSRecipient.EncodedWarehouse = tbLocationCode.Text;
            mSRecipient.Laboratory = tbLabCode.Value.Trim();
            mSRecipient.Name = lbCName.Text;
            mSRecipient.Numbers = tbCNub.Text.Trim();
            mSRecipient.OperatDate = DateTime.Now;
            mSRecipient.Operator = "";
            decimal iTemp1 = 0;
            decimal iTemp2 = 0;
             if (decimal.TryParse(tbUserNub.Text.Trim(), out iTemp1))
            {
                mSRecipient.QuantityRecipients = iTemp1.ToString();

                if (decimal.TryParse(mSRecipient.UnitPrice.Trim(), out iTemp2))
                {
                    iTemp1 *= iTemp2;
                    mSRecipient.TotalAmount = iTemp1.ToString();
                }
                else
                {
                    mSRecipient.TotalAmount = "0";
                }
                
            }
            mSRecipient.Recipients = tbRPersionCode.Value.Trim();
            mSRecipient.Words = lbTypeNub.Text.Trim();

            try
            {
                EbSRecipient.ExtendAdd(mSRecipient, iTotalNub);
            }
            catch (Exception ex)
            {
                lbErr.Visible = true;
                lbErr.Text = ex.Message.ToString();
                return;
            }
            LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "领用成功!", "consumsrecipients.aspx");
        }
    }
}
